// pages/conversionCard/conversionCard.js
var app = getApp();
var rootDocment = app.globalData.postUrl;
Page({
    /**
     * 页面的初始数据
     */
    data: {
        no: '',
        info: {},
        details: ' 马来西亚 D197猫山王榴莲 S1 AA果、马来西亚猫山王 榴莲果肉、榴莲冰激凌',
        notice: '2024.1.24日至2024.3.16日零点！',
        listIndex: [],
        listSpec: [],
        data_img: {
            bg_color: '#eee',
            text_color: '#fff',
            img: 'https://lhs.tann.cc/upload/images/conversionCard.png'
        },
        userInfo: {},
        keyobj: {},
        specList: {}
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log(options, 'options')
        // this.getCardInfo(7305)
        // 1122
        if (options.q && options.q != "undefined") {
            const qrUrl = decodeURIComponent(options.q)
            console.log(qrUrl, 'qrUrl');
            //此处就是我们要获取的参数 json，通过方法解析
            let jsonUrl = this.GetwxUrlParam(qrUrl);
            console.log(jsonUrl, 'jsonUrl');
            //比如我要得到id的值，直接取值即可
            let id = jsonUrl.id;
            this.getCardInfo(id)

        }
        // this.getCardInfo(1122)
        const res = wx.getMenuButtonBoundingClientRect()
        const statusHeight = res.top //胶囊距离顶部
        const navHeight = res.height //胶囊高度
        this.setData({
            statusHeight: statusHeight,
            navHeight: navHeight
        })
        console.log(navHeight, navHeight);
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },
    getCardInfo(id) {
        wx.showLoading({
            title: '加载中',
            mask: true
        })
        var that = this;
        var paraArr = new Array();
        paraArr['id'] = id;
        var sign = app.signature(paraArr);
        wx.request({
            url: rootDocment + '/api/redeem/index1',
            // '/api/redeem',
            data: {
                id: id,
                sign: sign
            },
            method: 'POST',
            header: {},
            success: function (res) {
                wx.hideLoading()
                console.log(res, '===>res')
                if (res.data.code == 1001) {
                    console.log(res, '商品信息')
                    let is_use = res.data.data.is_use
                    //let is_use =1
                    if (is_use == '1') {
                        app.gourl('orderSuccess/orderSuccess', 'title=已提货')
                    }
                    let keys = Object.keys(res.data.data.product.specList)
                    let keyobj = {}
                    keys.forEach((item, index) => {
                        keyobj[item] = 0
                    })

                    that.setData({
                        info: res.data.data,
                        keyobj: keyobj,
                        specList: res.data.data.product.specList
                    })
                } else {
                    wx.showToast({
                        title: res.data.msg,
                        icon: 'none',
                        duration: 3000
                    })
                }
            },
            fail: function (err) {
                console.log(err, '===>err')
                //wx.hideLoading()
                wx.showToast({
                    title: '请求失败',
                    icon: 'error'
                })
            }
        })
    },
    GetwxUrlParam(url) {
        let theRequest = {};
        if (url.indexOf("#") != -1) {
            const str = url.split("#")[1];
            const strs = str.split("&");
            for (let i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = decodeURI(strs[i].split("=")[1]);
            }
        } else if (url.indexOf("?") != -1) {
            const str = url.split("?")[1];
            const strs = str.split("&");
            for (let i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = decodeURI(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    },
    setIndex(e) {
        let {
            item_id,
            spec_id,
            index,
            key
        } = e.target.dataset
        // this.data.keyobj
        let obj = this.data.keyobj
        obj[key] = index
        this.setData({
            keyobj: obj,
        })

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        var that = this

    },
    to_home() {
        wx.switchTab({
            url: '/pages/index/index',
        })
    },
    goDetail(e) {
        let t = this
        wx.navigateTo({
            url: `/pages/goods/card_goods_detail?id=${t.data.info.product.id}`,
        })
    },
    goOrder() {
        console.log(this.data.keyobj);
        console.log(this.data.specList);
        let t = this
        let spec_keys = []
        let kyesList = Object.keys(this.data.keyobj)
        kyesList.forEach((item, index) => {
            console.log();
            let a = this.data.specList[item][this.data.keyobj[item]]
            spec_keys.push(a.item_id)
        })
        var spec_keya = spec_keys.sort(function (a, b) {
            return a - b
        }).join('_');
        wx.request({
            url: rootDocment + '/api/comm_get/getOrderGoods',
            data: {
                goods_id: this.data.info.product.id,
                spec_key: spec_keya,
                amount: 1,
            },
            method: 'GET',
            header: {},
            success: function (res) {
                t.to_order(spec_keya)

            },
            fail: function (err) {
                wx.showToast({
                    title: '未知错误',
                    icon: 'none'
                })
            }
        })


    },
    to_order(spec_keys) {
        var that = this
        app.login(function (userInfo) {
            that.setData({
                userInfo: userInfo
            });
        });
        if (!that.data.userInfo.userID) {
            return
        }

        let spec_key = spec_keys
        let amount = 1
        let type = 1
        if (this.data.info.number) {
            var cardId = this.data.info.number
            if (cardId == '') {
                wx.showToast({
                    title: '没有识别到卡号,请退出重新扫码',
                    icon: 'none'
                })
                return
            }
        } else {
            wx.showToast({
                title: '没有识别到卡号,请退出重新扫码',
                icon: 'none'
            })
            return
        }
        let goods_id = this.data.info.product.id
        if (goods_id == '') {
            wx.showToast({
                title: '请退出重试',
                icon: 'none'
            })
            return
        }
        app.redirect('order/index', 'cardId=' + cardId + '&goods_id=' + goods_id + '&spec_key=' + spec_key + '&amount=' + amount + '&type=' + type);
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})